package cn.itsource.hbc.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.hbc.org.domain.Employee;
import cn.itsource.hbc.org.domain.Shop;
import cn.itsource.hbc.org.domain.ShopLog;
import cn.itsource.hbc.org.mapper.EmployeeMapper;
import cn.itsource.hbc.org.mapper.ShopLogMapper;
import cn.itsource.hbc.org.mapper.ShopMapper;
import cn.itsource.hbc.org.service.IShopService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopLogMapper shopLogMapper;

    @Override
    public void settlement(Shop shop) {
        // 设置默认状态
        shop.getAdmin().setState(0);
        shop.setState(0);

        employeeMapper.save(shop.getAdmin());
        super.save(shop);


    }

    @SneakyThrows
    @Override
    public void reject(ShopLog shopLog) {

        // 先获取到店铺的id
        Shop shop = shopMapper.loadById(shopLog.getShop_id());
        // 设置驳回状态为-1
        shop.setState(-1);
        // 更新数据
        shopMapper.update(shop);
        // 设置修改人信息
        Employee employee = new Employee();
        employee.setId(1L);
        shopLog.setEmployee(employee);
        shopLogMapper.save(shopLog);

        //创建复杂邮件对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        Employee manager = employeeMapper.loadById(shop.getAdmin_id());
        //使用MimeMessageHelper对象来设置文件发送内容
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1005574023@qq.com");
        //设置邮件主题
        helper.setSubject("商家入驻资质审核失败");
        //使用html页面读取内容
        helper.setText("<h1>审核失败</h1><br>"+
                "<span>失败原因："+shopLog.getNote()+"</span><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>",true);
        // 收件人账号
        //   helper.setTo("569823059@qq.com");
        helper.setTo(manager.getEmail());
        //发送消息
        mailSender.send(mimeMessage);

    }

    @SneakyThrows
    @Override
    public void pass(ShopLog shopLog) {


        Shop shop = shopMapper.loadById(shopLog.getShop_id());
        // 设置通过状态为1
        shop.setState(1);
        // 更新数据
        shopMapper.update(shop);
        // 设置修改人信息
        Employee employee = new Employee();
        employee.setId(1L);
        shopLog.setEmployee(employee);
        shopLogMapper.save(shopLog);

        //创建复杂邮件对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        Employee manager = employeeMapper.loadById(shop.getAdmin_id());
        //使用MimeMessageHelper对象来设置文件发送内容
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1005574023@qq.com");
        //设置邮件主题
        helper.setSubject("商家入驻资质审核成功");
        //使用html页面读取内容
        helper.setText("<span>原因："+shopLog.getNote()+"</span>"+" <a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+">点击激活</a>",true);
        // 收件人账号
        //   helper.setTo("569823059@qq.com");
        helper.setTo(manager.getEmail());
        //发送消息
        mailSender.send(mimeMessage);

    }

    @Override
    public Shop loadByAdminId(Long id) {
        return shopMapper.loadByAdminId(id);
    }


}

